package com.taptech.doufu.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.util.ScreenUtil;

/**
 * Created by Wyatt on 2017/9/18/018.
 */

public class DoubleBtnTiptDialog extends Dialog{
    private TextView tvTip,tvBtnCancel,tvBtnSure,tvTitle;
    private String title,tip,leftBtnText,rightBtnText;
    private LinearLayout layContent;
    private SpannableStringBuilder spannableStringBuilder;
    private CallBack leftBtnCallBack,rightBtnCallBack;
    public DoubleBtnTiptDialog(@NonNull Context context, int theme, String tip, String rightBtnText, CallBack rightBtnCallBack) {
        super(context,theme);
        this.tip = tip;
        this.rightBtnText = rightBtnText;
        this.rightBtnCallBack = rightBtnCallBack;
    }

    public DoubleBtnTiptDialog(@NonNull Context context, int theme,String title, String tip, String rightBtnText, CallBack rightBtnCallBack) {
        super(context,theme);
        this.tip = tip;
        this.title = title;
        this.rightBtnText = rightBtnText;
        this.rightBtnCallBack = rightBtnCallBack;
    }

    public DoubleBtnTiptDialog(@NonNull Context context, int theme,String title, String tip,
                               String leftBtnText,String rightBtnText,CallBack leftBtnCallBack, CallBack rightBtnCallBack) {
        super(context,theme);
        this.tip = tip;
        this.title = title;
        this.leftBtnText = leftBtnText;
        this.rightBtnText = rightBtnText;
        this.leftBtnCallBack = leftBtnCallBack;
        this.rightBtnCallBack = rightBtnCallBack;
    }

    public DoubleBtnTiptDialog(@NonNull Context context, int theme, SpannableStringBuilder spannableStringBuilder, String rightBtnText, CallBack rightBtnCallBack) {
        super(context,theme);
        this.spannableStringBuilder = spannableStringBuilder;
        this.rightBtnText = rightBtnText;
        this.rightBtnCallBack = rightBtnCallBack;
    }


    public interface CallBack{
        void onBack();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_double_btn_tip);
        layContent = findViewById(R.id.layContent);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.getScreenWidth(getContext())-ScreenUtil.dip2px(getContext(),60f), ViewGroup.LayoutParams.WRAP_CONTENT);
        layContent.setLayoutParams(params);
        tvTip = (TextView) findViewById(R.id.tvTip);
        tvTitle = findViewById(R.id.tvTitle);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        }else{
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        if (spannableStringBuilder != null) {
            tvTip.setText(spannableStringBuilder);
        }else{
            tvTip.setText(tip);
        }
        tvBtnCancel = (TextView) findViewById(R.id.tvBtnCancel);
        tvBtnSure = (TextView) findViewById(R.id.tvBtnSure);
        if (!TextUtils.isEmpty(rightBtnText)) {
            tvBtnSure.setText(rightBtnText);
        }
        if (!TextUtils.isEmpty(leftBtnText)) {
            tvBtnCancel.setText(leftBtnText);
        }

        tvBtnCancel.setOnClickListener(v -> {
            dismiss();
            if (leftBtnCallBack != null) {
                leftBtnCallBack.onBack();
            }
        });
        tvBtnSure.setOnClickListener(v -> {
            dismiss();
            if (rightBtnCallBack != null) {
                rightBtnCallBack.onBack();
            }
        });
    }
}
